package com.maxjay.system.core.interceptor;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

import com.lowagie.text.pdf.codec.Base64;
import com.maxjay.common.exception.ERR;
import com.maxjay.common.exception.GeneException;

/**
 * 
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:页面视图状态保持器
 * 	
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author 洪桃李
 * @version 1.0
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class ViewState implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(ViewState.class);
	
	/**VIEWSTATE_KEY*/
	public static final String VIEWSTATE_KEY = "__VIEWSTATE";
	
	/**VIEWSTATE_KEY*/
	public static final String VIEWSTATE_PROPERTY_NAME = "viewState";
	
	/**states*/
	protected Map states = new HashMap();
	
	/**
	 * 设置Attribute
	 * @param key String
	 * @param obj Serializable
	 * @author:Administrator
	 */
	public void setAttribute(String key, Serializable obj) {
		states.put(key, obj);
	}
	
	/**
	 * 
	 * @return Iterator
	 * @author:Administrator
	 */
	public java.util.Iterator keyIterator() {
		return states.keySet().iterator();
	}
	
	/**
	 * getAttribute
	 * @param key String
	 * @return Serializable
	 * @author:Administrator
	 */
	public Serializable getAttribute(String key) {
		return (Serializable) states.get(key);
	}
	
	/**
	 * removeAttribute
	 * @param key String
	 * @author:Administrator
	 */
	public void removeAttribute(String key) {
		states.remove(key);
	}

	/**
	 * 解析页面状态视图
	 * 
	 * @param states String
	 * @return ViewState
	 * @author:Administrator
	 */
	public static ViewState parseViewState(String states) {
		try {
			java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(Base64.decode(states));
			java.io.ObjectInputStream ois = new java.io.ObjectInputStream(bis);
			ViewState vs = new ViewState();
			Object key = null;
			try {
				key = ois.readObject();
			} catch (IOException ex2) {
				logger.error("viewState中未保存任何属性");
			}
			while (key != null) {
				Object value = null;
				try {
					value = ois.readObject();
				} catch (Exception ex3) {
					throw GeneException.getGeneException( ERR.ERROR_ERR_MSG,"视图状态被破坏 key=" + key + " 消息:" + ex3.getMessage(), ex3);
				}
				vs.setAttribute((String) key, (Serializable) value);
				try {
					key = ois.readObject();
				} catch (Exception ex1) {
					key = null;
				}
			}
			return vs;
		} catch (GeneException ex) {
			throw ex;
		} catch (Exception ex) {
			throw GeneException.getGeneException( ERR.ERROR_ERR_MSG, "视图状态被破坏 " + ex.getMessage(), ex);
		}
	}
	
	/**
	 * 将页面存储对象序列字符串解析为相应的对象
	 * @return String
	 */
	public String toString() {
		Object key = null;
		try {
			java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
			java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(bos);
			Iterator iter = this.states.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry item = (Map.Entry) iter.next();
				try {
					oos.writeObject(item.getKey());
					oos.writeObject(item.getValue());
				} catch (Exception ex1) {
					key = item.getKey();
					throw ex1;
				}
			}
			return Base64.encodeBytes(bos.toByteArray());
		} catch (Exception ex) {
			throw GeneException.getGeneException( ERR.ERROR_ERR_MSG, "序列化视图状态出错! key=" + key + " 信息:" + ex.getMessage(), ex);
		}
	}

	/**
	 * main
	 * @param args String[]
	 */
	public static void main(String[] args) {
		ViewState vs = new ViewState();
		vs.setAttribute("a", "fds");
		vs.setAttribute("b", new HashMap());
		String s = vs.toString();
		System.out.println(s);
		ViewState v = ViewState.parseViewState(s);
		System.out.println(v.states.size());
		System.out.println(v.getAttribute("a"));
		System.out.println(v.getAttribute("b"));
		// Map states = new HashMap();
		// states.put("a","a");
		// states.put("a","b");
		// System.out.println(states.get("a"));
	}
}
